package com.ilibrary.db.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ilibrary.db.dao.MyBaseHibernateDao;
import com.ilibrary.db.dao.hibernate.BookDao;
import com.ilibrary.db.entity.Book;

public class BookDaoImpl extends MyBaseHibernateDao<Book, Integer> implements BookDao {

	@Override
	public List<Book> queryList(final int start, final int limit, String categoryId, String keywords) {
		
		List<Book> list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery("from Book b order by b.updateDt asc");
					query.setFirstResult(start);
					query.setMaxResults(limit);
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	@Override
	public Integer queryCnt(String categoryId, String keywords) {
		List<Book> list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery("from Book b");
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return list.size();
	}

	@Override
	public List<Book> queryListByKeyword(final int start, final int limit, final String keywords) {
		List<Book> list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery("from Book b where b.bookName like ? or b.bookAuthor like ? order by b.updateDt asc");
					query.setParameter(0, "%"+keywords+"%");
					query.setParameter(1, "%"+keywords+"%");
					query.setFirstResult(start);
					query.setMaxResults(limit);
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	@Override
	public Integer queryCntByKeyword(final String keywords) {
		List<Book> list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery("from Book b where b.bookName like ? or b.bookAuthor like ?");
					query.setParameter(0, "%"+keywords+"%");
					query.setParameter(1, "%"+keywords+"%");
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return list.size();
	}

}
